//
//  CustomerGroupViewController.m
//  WebPDA
//
//  Created by Justin Yip on 10-7-26.
//  Copyright 2010 TenNights.com. All rights reserved.
//

#import "PassengerGroupViewController.h"
#import "PassengerGroupCell.h"
#import "AllPassengerViewController.h"
#import "SeatGraphViewController.h"


@implementation PassengerGroupViewController

@synthesize menu;
@synthesize contentController;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    //self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	
	//初始化菜单
	NSString *menuPath = [[NSBundle mainBundle] pathForResource:MENU_NAME ofType:@"plist"];
	NSArray *menuArray = [[NSArray alloc] initWithContentsOfFile:menuPath];
	self.menu = menuArray;
	[menuArray release];
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return [menu count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.

	NSDictionary *sectionDict = [menu objectAtIndex:section];
	NSArray *rows = [sectionDict valueForKey:MENU_ROW_KEY];
	if (rows) {
		return [rows count];
	}
	return 0;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"PassengerGroupCell";
    
    PassengerGroupCell *cell = (PassengerGroupCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PassengerGroupCell" owner:self options:nil];
		for (id oneObject in nib) {
			if ([oneObject isKindOfClass:[PassengerGroupCell class]]) {
				cell = (PassengerGroupCell*)oneObject;
			}
		}
	}
    
    // Configure the cell...
	NSDictionary *sectionDict = [menu objectAtIndex:indexPath.section];
	NSArray *rows = [sectionDict valueForKey:MENU_ROW_KEY];
	NSDictionary *rowDict = [rows objectAtIndex:indexPath.row];
	
	// 取行数据
	NSString *groupName = [rowDict valueForKey:MENU_GROUP_NAME_KEY];
	NSString *count = [rowDict valueForKey:MENU_GROUP_COUNT_KEY];
	NSString *imageFile = [rowDict valueForKey:MENU_GROUP_IMAGE_KEY];
	
	cell.groupLabel.text = groupName;
	cell.countLabel.text = count;
	UIImage *image = [UIImage imageNamed:imageFile];
	if (image) {
		cell.imageView.image = image;
	}
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	
	UIViewController *detailVC = nil;
	AllPassengerViewController *tmpDetailVC = nil;
	switch (indexPath.section) {
		case 0:
			//舱位图
			detailVC = [[SeatGraphViewController alloc] init];
			
			break;
		case 1:
			//登机牌
			
			break;
		default:
			//旅客
			tmpDetailVC = [[AllPassengerViewController alloc] initWithNibName:@"AllPassengerViewController" bundle:nil];
			tmpDetailVC.contentController = self.contentController;
			detailVC = tmpDetailVC;
			break;
	}
	
	if (detailVC) {
		[self.navigationController pushViewController:detailVC animated:YES];
		[detailVC release];
	}
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
	[contentController release];
	[menu release];
	[super dealloc];
}


@end

